##################################################################################################################################
#
# Copyright (C) 2008-2012 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_client {} {
	set values [$::daidze_main get_values]

	set msg_list [dict create name "Client name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		$::db insert_all client $values
		$::daidze_main load_main client client.xml $::main 1
	}
}

proc edit_client {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]

	set vars {id name contact street street_detail city province country phone mobile fax email web fin}
	set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
	foreach key $vars value $row {
		dict set sdata $key $value
	}
	$::daidze_main load_main client edit_client.xml $::main
	$::daidze_main update_form $sdata
}

proc update_client {} {
	set values [$::daidze_main get_values]

	set msg_list [dict create name "Client name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { 
			lappend flags 1
		}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values id] != 1} {
			$::db update_all client $values
		} else {
			tk_messageBox -icon info -type ok -message "[::msgcat::mc "This is default client in the system. You can not edit this entry."]"
		}
		$::daidze_main load_main client client.xml $::main 1
	 }
}



proc send_client_email_form {} {
	set mail_win [top_win "Send email to client"]
	set mail_frame [ttk::frame $mail_win.top]
	set ::daidze_mail [xml_wrapper new]
	pack $mail_frame
	$::daidze_mail load_main client email_client.xml $mail_frame
	$::daidze_mail update_form [dict create daidze_info_mail support@daidze.com] 
}

proc select_all {table_var} {
	set my_tablelist [dict get [$::daidze_mail get_fdata] $table_var]
	$my_tablelist selection set 0 end
}

proc deselect_all {table_var} {
	set my_tablelist [dict get [$::daidze_mail get_fdata] $table_var]
	$my_tablelist selection clear 0 end
}

proc send_client_email {} {
	set data [$::daidze_mail get_values list subject body]
	puts $data
	set recipients [lindex [$::daidze_mail get_values list client] 0]
	set t1 [thread::create]
	foreach line $recipients {
		set send_list [list [lindex $line 1] [lindex $data 0] [lindex $data 1]]
		thread::send -async $t1 [send_email {*}$send_list]
	}

}